package com.example.inteviewwhy.controller;

import com.example.inteviewwhy.entity.User;
import com.example.inteviewwhy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "添加/更新用户")
    @PostMapping("/save")
    public User save(User user) {
        if (user == null) {
            return null;
        }
       return userService.save(user);
    }

    @ApiOperation(value = "根据uid删除用户")
    @PostMapping("/delete/{uid}")
    public Map delete(@PathVariable Long uid) {
        Map map = new HashMap();
        if (uid == null) {
            map.put("message","无该对象");
            return map;
        }
        User user = new User();
        user.setUid(uid);
        userService.delete(user);
        map.put("message","Success!数据已不存在");
        return map;
    }

    @ApiOperation(value = "根据姓名查询用户列表")
    @PostMapping("/getList")
    public List<User> get(String name){
        if ("".equals(name)){
            return new ArrayList<>();
        }
        List<User> list = userService.findAll(name);
        return list;
    }

    @ApiOperation(value = "根据id查询用户信息详情")
    @PostMapping("/getOneDetail/{uid}")
    public Object get(@PathVariable Long uid){
        if ("".equals(uid)){
            return null;
        }
        return userService.findDetail(uid);
    }
}
